/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanRelation;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.vo.MaintainPlanRelationVO;
import org.springblade.modules.repair.mapper.MaintainPlanRelationMapper;
import org.springblade.modules.repair.service.IMaintainPlanRelationService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务关联表 服务实现类
 *
 * @author ysq
 * @since 2022-03-29
 */
@Service
public class MaintainPlanRelationServiceImpl extends ServiceImpl<MaintainPlanRelationMapper, MaintainPlanRelation> implements IMaintainPlanRelationService {

	@Autowired
	private IMaintainPlanContentService maintainPlanContentService;

	@Override
	public IPage<MaintainPlanRelationVO> selectMaintainPlanRelationPage(IPage<MaintainPlanRelationVO> page, MaintainPlanRelationVO maintainPlanRelation) {
		return page.setRecords(baseMapper.selectMaintainPlanRelationPage(page, maintainPlanRelation));
	}

	@Override
	public String getPlanLinks(Long currentPlanId) {
		Long ownId = currentPlanId;
		List<Integer> planLinks = new ArrayList<>();
		while (!Func.isNull(
			this.getOne(new LambdaQueryWrapper<MaintainPlanRelation>()
				.eq(MaintainPlanRelation::getOwnPlanId, ownId))
		)){
			MaintainPlanRelation currentRelation = this.getOne(new LambdaQueryWrapper<MaintainPlanRelation>()
				.eq(MaintainPlanRelation::getOwnPlanId, ownId));
			MaintainPlanContent fromPlan = maintainPlanContentService.getOne(new LambdaQueryWrapper<MaintainPlanContent>()
				.eq(BaseEntity::getId, currentRelation.getFromPlanId())
			);
			Integer typeStatus = fromPlan.getTypeStatus();
			planLinks.add(typeStatus);
			ownId = currentRelation.getFromPlanId();
		}
		if (!planLinks.isEmpty()) {
			return publicAppend(planLinks, ";");
		}
		return null;
	}

	@Override
	public String getPlanIds(Long currentPlanId) {
		Long ownId = currentPlanId;
		List<Long> planIds = new ArrayList<>();
		while (!Func.isNull(
			this.getOne(new LambdaQueryWrapper<MaintainPlanRelation>()
				.eq(MaintainPlanRelation::getOwnPlanId, ownId))
		)){
			MaintainPlanRelation currentRelation = this.getOne(new LambdaQueryWrapper<MaintainPlanRelation>()
				.eq(MaintainPlanRelation::getOwnPlanId, ownId));
			MaintainPlanContent fromPlan = maintainPlanContentService.getOne(new LambdaQueryWrapper<MaintainPlanContent>()
				.eq(BaseEntity::getId, currentRelation.getFromPlanId())
			);
			planIds.add(fromPlan.getId());
			ownId = currentRelation.getFromPlanId();
		}
		if (!planIds.isEmpty()) {
			return publicAppend(planIds, ";");
		}
		return null;
	}

	/**
	 * 公共方法抽取：拼接
	 */
	private String publicAppend(List<?> list, String separator){
		if (!list.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			for (int i = list.size() - 1; i >= 0; i--) {
				sb.append(list.get(i));
				sb.append(separator);
			}
			return sb.toString();
		}
		return null;
	}

}
